package com.itheima.mall.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class PageResult implements Serializable {

    private Integer total = 0;//总记录数
    private Integer pageSize;//页大小
    private Integer totalPage = 0;//总页数
    private Integer pageNum;//当前页码
    private List<?> list = Collections.emptyList(); //列表

    public PageResult(Integer total, Integer pageSize, Integer pageNum, List list) {
        this.total = total;
        this.pageSize = pageSize;
        this.totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        this.pageNum = pageNum;
        this.list = list;
    }

    public static PageResult init(Page page) {
        PageResult pr = new PageResult();
        if (page != null) {
            pr.setTotal((int) page.getTotal())
                    .setPageNum((int) page.getCurrent())
                    .setTotalPage((int) page.getPages())
                    .setPageSize((int) page.getSize())
                    .setList(page.getRecords());
        }
        return pr;
    }
}